//
//  WSJWoundDesVC.m
//  WSJWoundDesVC
//
//  Created by wsj2015 on 16/3/30.
//  Copyright © 2016年 wsj2015. All rights reserved.
//

#import "WSJWoundDesVC.h"
#import "WSJReadPlistTool.h"
#import "WoundDesModel.h"
#import "WoundDesCell.h"
#import "WoundContentModel.h"
#import "TPKeyboardAvoidingTableView.h"
#import "WoundDesCellEditLeft.h"
#import "WoundDesCellEditEnd.h"
#import "WoundDesCellEditTwo.h"
#import "JCAlertView.h"
#import "WCButton.h"
#import "UINavigationItem+Offset.h"
@interface WSJWoundDesVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,  strong) NSMutableArray *dataArray;

@property (nonatomic,  strong) TPKeyboardAvoidingTableView *tableView;
@property (nonatomic,  strong) NSMutableDictionary *allCells;


@end

@implementation WSJWoundDesVC

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView =[[TPKeyboardAvoidingTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height -64) style:UITableViewStyleGrouped];
        _tableView.delegate =self;
//        _tableView.backgroundColor =[UIColor redColor];
        _tableView.dataSource  =self;
        
    }
    return _tableView;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.allCells = [NSMutableDictionary dictionary];
        self.title = @"伤口描述";
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self.tableView registerNib:[UINib nibWithNibName:@"WoundDesCell" bundle:nil] forHeaderFooterViewReuseIdentifier:@"WoundDesCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WoundDesCell" bundle:nil] forCellReuseIdentifier:@"WoundDesCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WoundDesCellEditLeft" bundle:nil] forCellReuseIdentifier:@"WoundDesCellEditLeft"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WoundDesCellEditEnd" bundle:nil] forCellReuseIdentifier:@"WoundDesCellEditEnd"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WoundDesCellEditTwo" bundle:nil] forCellReuseIdentifier:@"WoundDesCellEditTwo"];
    
    [self.view addSubview:self.tableView];
    self.dataArray = [NSMutableArray arrayWithArray:[WSJReadPlistTool getModelArrayFromPlist]];
    
    
    WCButton *rightBtn = [WCButton wcButtonWithType:WCButtonTypeSave];
    [rightBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem addRightBarButtonItem:rightBtn];
    
    
}

- (void)pageBack
{
    weakSelf_define;
    [self.view endEditing:YES];
    [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"是否放弃编辑" ButtonType:2 ButtonTitle:@"放弃" Click:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } ButtonType:1 ButtonTitle:@"继续" Click:^{
        
    }];
 
}



- (void)begain{
    for (WoundDesModel *section in self.dataArray){
        for (WoundContentModel *content in section.desArray) {
            if (content.isSelect == YES) {
                section.isHasSelectedItem =YES;
                continue;
            }
        }
    }
    
    //-----------------------------------------//
    NSMutableArray *secArray =[NSMutableArray array];
    NSMutableArray *titleArray =[NSMutableArray array];
    for (WoundDesModel *section in self.dataArray) {
        NSMutableArray *subArr =[NSMutableArray array];
        for (WoundContentModel *content in section.desArray) {
            
            
            if (content.isSelect == YES) {
                if (![titleArray containsObject:section.sectionTitle]) {
                    [titleArray addObject:section.sectionTitle];
                }
                
                [subArr addObject:content];
                
            }
            
        }
        if (subArr.count>0) {
            [secArray addObject:subArr];
        }
        
    }
    
    
    
    //-----------------------------------------//
    NSMutableString *targetStr =[NSMutableString string];
    for (NSArray *subArr in secArray) {
        
        [targetStr appendFormat:@"%@:",titleArray[[secArray indexOfObject:subArr]]];
        
//        __block int i = 1;
        
        
        [subArr enumerateObjectsUsingBlock:^(WoundContentModel *content, NSUInteger idx, BOOL * _Nonnull stop) {
            if (content.readonly == YES) {
                [targetStr appendFormat:@"%@",content.content];
                
            }else{
                
                
                NSMutableString *temStr =[NSMutableString string];
                if (content.enditType ==1) {
//                    [temStr appendFormat:@"",i++];
                    [temStr appendString:content.editStrO];
                    
                    [temStr appendString:content.content];
                    [temStr appendString:@"  "];
                }else if(content.enditType ==2){
                    [targetStr appendFormat:@"%@",content.content];
                    
                    [temStr appendString:content.editStrT];
                    [temStr appendString:@"  "];
                    
                }else if(content.enditType ==3){
                    
                    NSArray *resultArr =[content.content componentsSeparatedByString:kKeySepcicalWord];
                    if (resultArr.count==3) {
                        [temStr appendString:resultArr[0]];
                        [temStr appendString:@":"];
                        [temStr appendString:content.editStrO];
                        [temStr appendString:resultArr[1]];
                        [temStr appendString:@":"];
                        [temStr appendString:content.editStrT];
                        [temStr appendString:resultArr[2]];
                        [temStr appendString:@"  "];
                    }
                    
                }else {     //第一种
                    [targetStr appendFormat:@"%@",content.content];
                    [temStr appendString:@"  "];
                    
                }
                [targetStr appendString:temStr];
                
            }
            
            if (idx ==subArr.count-1) {
                [targetStr appendString:@"\n"];
                [targetStr appendString:@"\n"];
            }
            
        }];
  
    }
    //    }
    
  targetStr = [[targetStr stringByReplacingOccurrencesOfString:@"?" withString:@""] mutableCopy];
   
    if (![targetStr isEqualToString:@""]) {
        //NSLog(@"%@",[targetStr substringWithRange:NSMakeRange([targetStr length] - 2, 2)]);
        
            
        [targetStr replaceCharactersInRange:NSMakeRange([targetStr length] - 2, 2) withString:@""];
    
        self.callBackStr(targetStr);
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        WSJSHOW(@"内容不能为空");
        
    }
//    NSLog(@"%@",targetStr);
    
    



}

- (void)save{
    weakSelf_define;
    [self.view endEditing:YES];
    [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"是否确认" ButtonType:1 ButtonTitle:@"否" Click:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } ButtonType:0 ButtonTitle:@"确认提交" Click:^{
        [weakSelf begain];
    }];
    
    
    
  
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    NSLog(@"%li",self.dataArray.count);
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    WoundDesModel *model =self.dataArray[section];
   
    return model.isfolden?0:model.desArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WoundDesModel *model = self.dataArray[indexPath.section];
    WoundContentModel *content = model.desArray[indexPath.row];
    WoundDesBaseCell *idCell;
   
    if (content.enditType ==0) {
        WoundDesCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WoundDesCellId" forIndexPath:indexPath];
        idCell =cell;
    }else if (content.enditType ==1){
        WoundDesCellEditLeft *cell =[tableView dequeueReusableCellWithIdentifier:@"WoundDesCellEditLeft" forIndexPath:indexPath];
        idCell =cell;
    }else if(content.enditType ==2){
        WoundDesCellEditEnd *cell =[tableView dequeueReusableCellWithIdentifier:@"WoundDesCellEditEnd" forIndexPath:indexPath];
        idCell =cell;
    
    }
    else if(content.enditType ==3){
        WoundDesCellEditTwo *cell =[tableView dequeueReusableCellWithIdentifier:@"WoundDesCellEditTwo" forIndexPath:indexPath];
        idCell =cell;
        
    }
//     else if (content.enditType ==2){
//     }
//     else if (content.enditType ==3){
//     }

    idCell.model = content;
    idCell.selectionStyle = UITableViewCellSelectionStyleNone;
    idCell.btnBlock = ^(BOOL isSelect){
        
        content.isSelect =!content.isSelect;
    };
  
    //所有cell都保存起来
//    if(![self.allCells objectForKey:indexPath]){
//        self.allCells[indexPath] = idCell;
//    };
//    
    
    return idCell;

//    }
//    return nil;
    

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WoundDesModel *model = self.dataArray[indexPath.section];
    WoundContentModel *content = model.desArray[indexPath.row];
    if (content.readonly ==YES) {
        content.isSelect =!content.isSelect;
        [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
    }
   
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//    Arrow_Right
    WoundDesModel *model = self.dataArray[section];
    
    UIView *bgView =[UIView new];
    bgView.frame = CGRectMake(0, 0,self.view.bounds.size.width, 33);
    UIImageView *imageView =[UIImageView new];
    imageView.frame = CGRectMake(5, 10, 12, 12);
    imageView.image =model.isfolden?[UIImage imageNamed:@"Arrow_Right"]:[UIImage imageNamed:@"Arrow_Down"];
    [bgView addSubview:imageView];
    
    UILabel *label =[UILabel new];
    label.frame = CGRectMake(30, 3, self.view.bounds.size.width-40, 30);
    label.text =model.sectionTitle;
    [bgView addSubview:label];
    
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = 1000+ section;
    btn.backgroundColor = [UIColor clearColor];
    btn.frame = bgView.bounds;
    [btn addTarget:self action:@selector(foldAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:btn];
    
    return bgView;
  
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView  *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}
- (void)foldAction:(UIButton *)btn{
    NSInteger section =btn.tag -1000;
    WoundDesModel *model =self.dataArray[section];
    model.isfolden = !model.isfolden;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
}
@end
